package com.lxr.test5002;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.lxr.test5002.utils.MyUtil;

/**
 * 类描述:[LCD屏幕测试]
 * 说明:
 *
 * @author: LXR
 * @Date: 2023/12/14 16:08
 */
public class LCDActivity extends Activity implements View.OnClickListener {
    private static final int RED = Color.RED;
    private static final int GREEN = Color.GREEN;
    private static final int BLUE = Color.BLUE;
    private static final int BLACK = Color.BLACK;
    private static final int WHITE = Color.WHITE;
    private ImageView iv_lcd_bg;
    private TextView tv_lcd_tip;
    private SeekBar sb_lcd_brightness;
    private int mCurrState = RED;
    private static final int MAX_BRIGHTNESS = 255;
    Resources resources;
    private boolean maxChanged, minChanged;
    private TextView mTips;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_lcd);
        init();
    }
    private void init() {
        resources = this.getResources();
        iv_lcd_bg = (ImageView) findViewById(R.id.iv_lcd_bg);
        tv_lcd_tip = (TextView) findViewById(R.id.tv_lcd_tip);
        mTips = findViewById(R.id.tips);
        sb_lcd_brightness = (SeekBar) findViewById(R.id.sb_lcd_brightness);
        sb_lcd_brightness.setMax(MAX_BRIGHTNESS);
        sb_lcd_brightness.setProgress(getSystemBrightness());
        sb_lcd_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                changeAppBrightness(progress);
                if (progress == 0) {
                    minChanged = true;
                }
                if (progress == MAX_BRIGHTNESS) {
                    maxChanged = true;
                }
                if (minChanged && maxChanged) {
//                    Constants.showDailog(LCDActivity.this,
//                            "是否保存此次测试结果", handler, Gravity.TOP);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        iv_lcd_bg.setOnClickListener(this);
        iv_lcd_bg.setBackgroundColor(RED);
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_lcd_bg) {
            switch (mCurrState) {
                case RED:
                    iv_lcd_bg.setBackgroundColor(GREEN);
                    mCurrState = GREEN;
                    break;
                case GREEN:
                    iv_lcd_bg.setBackgroundColor(BLUE);
                    mCurrState = BLUE;
                    break;
                case BLUE:
                    iv_lcd_bg.setBackgroundColor(BLACK);
                    tv_lcd_tip.setTextColor(Color.WHITE);
                    mCurrState = BLACK;
                    break;
                case BLACK:
                    iv_lcd_bg.setBackgroundColor(WHITE);
                    tv_lcd_tip.setTextColor(Color.BLACK);
                    iv_lcd_bg.setClickable(false);
                    tv_lcd_tip.setText("请滑动调节屏幕亮度");
                    mCurrState = WHITE;
                    sb_lcd_brightness.setVisibility(View.VISIBLE);
                    mTips.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    private int getSystemBrightness() {
        int systemBrightness = 0;
        try {
            systemBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return systemBrightness;
    }

    private void changeAppBrightness(int brightness) {
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (brightness == -1) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
            lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
        }
        window.setAttributes(lp);
    }
}